package com.uhealin;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

public class Hotel implements WritableComparable<Hotel>,Cloneable{

	public static int RADIU=1000;
	
	public static double calDistance(Hotel h1,Hotel h2) {
		return Math.sqrt(
				 Math.pow(h1.getX().get()-h2.getX().get(),2)
				+Math.pow(h1.getY().get()-h2.getY().get(), 2));
				
	}
	
	private IntWritable type=new IntWritable()
			,id=new IntWritable()
			,x=new IntWritable()
			,y=new IntWritable()
			,xa=new IntWritable()
			,ya=new IntWritable();
	

	public void setXYa(int xd,int yd){
		this.xa.set(this.x.get()/RADIU+xd);
		this.ya.set(this.y.get()/RADIU+yd);
	}
	
	public void setHotal(Text str){
		String[] v=str.toString().split("\t");
		type.set(Integer.parseInt(v[0]));
		id.set(Integer.parseInt(v[1]));
		x.set(Integer.parseInt(v[2]));
		y.set(Integer.parseInt(v[3]));
		xa.set(x.get()/RADIU);
		ya.set(y.get()/RADIU);
		//type.set(Integer.parseInt(v[6]));
	}

	public void write(DataOutput out) throws IOException {
		// TODO 자동 생성된 메소드 스텁
		out.writeInt(type.get());
		out.writeInt(id.get());
		out.writeInt(x.get());
		out.writeInt(y.get());
		out.writeInt(xa.get());
		out.writeInt(ya.get());
	}

	public void readFields(DataInput in) throws IOException {
		// TODO 자동 생성된 메소드 스텁
		type=new IntWritable(in.readInt());
		id=new IntWritable(in.readInt());
		x=new IntWritable(in.readInt());
		y=new IntWritable(in.readInt());
		xa=new IntWritable(in.readInt());
		ya=new IntWritable(in.readInt());
		
	}

	public int compareTo(Hotel o) {
		// TODO 자동 생성된 메소드 스텁
		int re=0;
		
		if(this.getXa().get()>o.getXa().get()){
			re=1;
			
		}else if(this.getXa().get()<o.getXa().get()){
			re=-1;
		}else{
			
			if(this.getYa().get()>o.getYa().get()){
				
				re=1;
				
			}else if(this.getYa().get()<o.getYa().get()){
				
				re=-1;
				
			}else{
				re=this.getType().get()-o.getType().get();
			}
			
		}
		
		return re;
	}
	
	
	
	
	@Override
	public String toString() {
		return "Hotel [type=" + type + ", id=" + id + ", x=" + x + ", y=" + y + ", xa=" + xa + ", ya=" + ya + "]";
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		// TODO 자동 생성된 메소드 스텁
		return super.clone();
	}

	public IntWritable getType() {
		return type;
	}

	public IntWritable getId() {
		return id;
	}

	public IntWritable getX() {
		return x;
	}

	public IntWritable getY() {
		return y;
	}

	public IntWritable getXa() {
		return xa;
	}

	public IntWritable getYa() {
		return ya;
	}
}
